/**
  ******************************************************************************
  * @file    jtt808.c
  * @author  lishelin
  * @version V1.0.1
  * @date    XX-XX-2017
  * @brief   
  ******************************************************************************
  * @attention
  *
	*	MIT License
	*
	*	Permission is hereby granted, free of charge, to any person obtaining a copy
	*	of this software and associated documentation files (the "Software"), to deal
	*	in the Software without restriction, including without limitation the rights
	*	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	*	copies of the Software, and to permit persons to whom the Software is
	*	furnished to do so, subject to the following conditions:

	*	The above copyright notice and this permission notice shall be included in all
	*	copies or substantial portions of the Software.

	*	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	*	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	*	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	*	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	*	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	*	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	*	SOFTWARE.
    
    *
    * Change Logs:
    * Date           Author       Notes
    * 2017-XX-XX     lishelin     the first version    
    
    
  *
  * <h2><center>&copy; COPYRIGHT 2017 lishelin0505@163.com</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/

//BIG-ENDIAN 

#include "jtt808.h"

#ifdef  JTT808_MODULE

/*! *  ensure the space is enough to avoid memory overflow 
    *  because it won't be checked in this version
*/
#define DATA_LEN   512 

BYTE gData[DATA_LEN];
 


/**
  * @brief  终端通用应答
  * @param  serialID 应答流水号
  * @param  answerID 应答ID
  * @param  result	 结果
  * @retval length of data
  */
static int dev_response(WORD serialID,WORD answerID,BYTE result)
{
    int len = -1;
    
    WORD msgID = DEV_CMD_RESPONSE;
	gData[0] = (BYTE) (msgID>>8);
	gData[1] = (BYTE) (msgID);
    
    gData[2] = (BYTE) (serialID>>8);
    gData[3] = (BYTE) (serialID);

    gData[4] = (BYTE) answerID>>8;
    gData[5] = (BYTE) (answerID);

    gData[6] = result;

    len = 7;
    return len;
}

/**
  * @brief  终端心跳
  * @param  None
  * @retval length of data
  */
static int dev_heart_beat(void)
{
	int len = -1;
    
    WORD msgID = DEV_CMD_HEART_BEAT;
	gData[0] = (BYTE) (msgID>>8);
	gData[1] = (BYTE) (msgID);
	
	len = 2;
	return len;
}

/**
  * @brief  终端注销
  * @param  None
  * @retval length of data
  */
static int dev_unregister(void)
{
	int len = -1;
	WORD msgID = DEV_CMD_UNREGISTER;
	gData[0] = (BYTE) (msgID>>8);
	gData[1] = (BYTE) (msgID);
	
	len = 2;
	return len;
}


/**
  * @brief  终端鉴权
  * @param  authentication_code 鉴权码
  * @param  length 鉴权码长度
  * @retval length of data
  */
static int dev_authentication(STRING authentication_code)
{
	int len = -1;
	int i,ret;
	WORD msgID = DEV_CMD_AUTHENTICATION;
	gData[0] = (BYTE) (msgID>>8);
	gData[1] = (BYTE) (msgID);	
    
    ret = strlen(authentication_code);
    if(ret>0)
    {
    
	for(i=0;i<ret;i++)
		gData[2+i] = authentication_code[i];
    }
    else
        return -1;
	
	len = ret +2;
	return len;
}

/**
  * @brief  终端参数
  * @param  serialID 应答流水号
  * @param  argNum 应答参数个数
  * @param  dev_arg 参数项列表
  * @retval length of data
  */
static int dev_parameter(WORD serialID,BYTE argNum,STR_PARAMETER *dev_arg)
{
	int len = -1;
	BYTE i,k;
	WORD j;
    
    WORD msgID = DEV_CMD_PARAMETER;
	gData[0] = (BYTE) (msgID>>8);
	gData[1] = (BYTE) (msgID);
  
	gData[2] = (BYTE) serialID>>8;
    gData[3] = (BYTE) (serialID);
	
	gData[4] = argNum;
	
	k = 0;
	for(i=0;i<argNum;i++)
	{
		for(j=0;j<dev_arg->data[i].count;j++)
        {
            gData[5+k] = dev_arg->data[i].idList[j];
        }
        k += dev_arg->data[i].count;
	}
	
	len = k+5;
	return len;
}

/**
  * @brief  查询终端属性应答
  * @param  attr 终端属性
  * @retval length of data
  */
static int dev_attribute(STR_DEV_ATTR attr)
{
	int len = -1;
	int i,j;
    
    WORD msgID = DEV_CMD_ATTRIBUTE;
	gData[0] = (BYTE) (msgID>>8);
	gData[1] = (BYTE) (msgID);
	
	gData[2] = (BYTE)(attr.dev_type>>8);
    gData[3] = (BYTE) (attr.dev_type);
	
	for(i=0;i<5;i++)
		gData[4+i] = attr.manufacturer_ID[i];
	for(i=0;i<20;i++)
		gData[9+i] = attr.dev_model[i];
	for(i=0;i<7;i++)
		gData[29+i] = attr.dev_ID[i];
	for(i=0;i<10;i++)
		gData[44+i] = attr.ICCID[i];
	
	gData[54] = attr.dev_version_len;
	for(i=0;i<attr.dev_version_len;i++)
		gData[55+i] = attr.dev_version[i];
	j = attr.dev_version_len;
	
	gData[55+j] = attr.firmware_version_len;
	for(i=0;i<attr.firmware_version_len;i++)
		gData[56+j] = attr.firmware_version[i];	
	j += attr.firmware_version_len;
	
	gData[56+j] = attr.gnns;
	gData[57+j] = attr.communication_module;	
	
	len = 57+j+1;
	return len;
}


/**
  * @brief  位置信息汇报
  * @param  serialID 应答流水号
  * @param  location 位置基本信息
  * @retval length of data
  */
static int dev_location(WORD serialID,STR_LOCATION_INF location)
{
 	int len = -1;
    int i;
    
    WORD msgID = DEV_CMD_LOCATION;
	gData[0] = (BYTE) (msgID>>8);
	gData[1] = (BYTE) (msgID);
    
	gData[2] = (BYTE) (serialID>>8);
    gData[3] = (BYTE) (serialID);  

	gData[4] = (BYTE) (location.alarm_flag >> 24);
    gData[5] = (BYTE) (location.alarm_flag >> 16);  
 	gData[6] = (BYTE) (location.alarm_flag  >> 8);
    gData[7] = (BYTE) (location.alarm_flag );     

	gData[8] = (BYTE) (location.status >> 24);
    gData[9] = (BYTE) (location.status >> 16);  
 	gData[10] = (BYTE) (location.status >> 8);
    gData[11] = (BYTE) (location.status );     
    
	gData[12] = (BYTE) (location.latitude >> 24);
    gData[13] = (BYTE) (location.latitude >> 16);  
 	gData[14] = (BYTE) (location.latitude >> 8);
    gData[15] = (BYTE) (location.latitude ); 
   
	gData[16] = (BYTE) (location.longitude>> 24);
    gData[17] = (BYTE) (location.longitude  >> 16);  
 	gData[18] = (BYTE) location.longitude >> 8;
    gData[19] = (BYTE) (location.longitude ); 
    
	gData[20] = (BYTE) (location.high >> 8);
    gData[21] = (BYTE) (location.high );  

	gData[22] = (BYTE) (location.speed >> 8);
    gData[23] = (BYTE) (location.speed );  

	gData[24] = (BYTE) (location.direction >> 8);
    gData[25] = (BYTE) (location.direction );  
    
    for(i=0;i<6;i++)
        gData[26+i] = location.time[i];
    
    len = 32;
	return len;    
}

/**
  * @brief  终端升级结果通知
  * @param  uploadType 升级类型
  * @param  uploadResult 升级结果
  * @retval length of data
  */
static int dev_upload_result(BYTE uploadType,BYTE uploadResult)
{
    int len =-1;
    WORD msgID = DEV_CMD_UPLOAD_RESULT;
	gData[0] = (BYTE) (msgID>>8);
    gData[1] = (BYTE) (msgID);  
    
    gData[2] =  uploadType;
    gData[3] =  uploadResult;  

    len = 4;
    
    return len;   
}

/**
  * @brief  事件报告
  * @param  eventID 事件ID
  * @retval length of data
  */
static int dev_event_report(BYTE eventID)
{
    int len =-1;
    WORD msgID = DEV_CMD_EVENT_REPORT;
	gData[0] = (BYTE) (msgID>>8);
    gData[1] = (BYTE) (msgID);  

	gData[2] = eventID; 

    len = 3;
    
    return len;    
}

/**
  * @brief  提问应答
  * @param  answerSerialID 应答流水号
  * @param  answerID 答案ID
  * @retval length of data
  */
static int dev_ask_answer(WORD answerSerialID,BYTE answerID)
{
    int len =-1;
    WORD msgID = DEV_CMD_ASK_ANSWER;
	gData[0] = (BYTE) (msgID >>8);
    gData[1] = (BYTE) (msgID);  

	gData[2] = (BYTE) (answerSerialID>>8);
    gData[3] = (BYTE) (answerSerialID);  
    
    gData[4] =  answerID;

    len = 5;
    
    return len;    
}

/**
  * @brief  信息点播/取消
  * @param  msgType 信息类型
  * @param  msgFlag 点播/取消标志
  * @retval length of data
  */
static int dev_msg_set(BYTE msgType,BYTE msgFlag)
{
    int len =-1;
    
    WORD msgID = DEV_CMD_MSG_SET;
	gData[0] = (BYTE) (msgID>>8);
    gData[1] = (BYTE) (msgID);  

	gData[2] = msgType; 
	gData[3] = msgFlag; 
    
    len = 4;
    
    return len;    
}


/**
  * @brief  车辆控制应答
  * @param  answerSerialID 信息类型
  * @param  msgFlag 点播/取消标志
  * @retval length of data
  */
static int dev_control_car_ret(WORD answerSerialID,STR_AREA_MSG msgFlag)
{
    int len =-1;
    int i,j,k;
    
    WORD msgID = DEV_CMD_CONTROL_CAR_RET;
	gData[0] = (BYTE) (msgID>>8);
    gData[1] = (BYTE) (msgID);  

	gData[2] = (BYTE) (answerSerialID>>8);
    gData[3] = (BYTE) (answerSerialID);  
    
    gData[4] =  (BYTE) msgFlag.attr;
    gData[5] =  (BYTE) msgFlag.count;  
    
    k=0;
    for(i=0;i<msgFlag.count;i++)
    {
        gData[6+k] =  (BYTE) (msgFlag.area[i].area_id >> 24);  
        gData[7+k] =  (BYTE) (msgFlag.area[i].area_id >> 16);  
        gData[8+k] =  (BYTE) (msgFlag.area[i].area_id >> 8);  
        gData[9+k] =  (BYTE) (msgFlag.area[i].area_id >> 0);  
        
        gData[10+k] =  (BYTE) (msgFlag.area[i].area_arrt >> 8);  
        gData[11+k] =  (BYTE) (msgFlag.area[i].area_arrt >> 0);  

        gData[12+k] =  (BYTE) (msgFlag.area[i].center_longitude >> 24);  
        gData[13+k] =  (BYTE) (msgFlag.area[i].center_longitude >> 16);  
        gData[14+k] =  (BYTE) (msgFlag.area[i].center_longitude >> 8);  
        gData[15+k] =  (BYTE) (msgFlag.area[i].center_longitude >> 0);

        gData[16+k] =  (BYTE) (msgFlag.area[i].center_latitude >> 24);  
        gData[17+k] =  (BYTE) (msgFlag.area[i].center_latitude >> 16);  
        gData[18+k] =  (BYTE) (msgFlag.area[i].center_latitude >> 8);  
        gData[19+k] =  (BYTE) (msgFlag.area[i].center_latitude >> 0);  

        gData[20+k] =  (BYTE) (msgFlag.area[i].radial >> 24);  
        gData[21+k] =  (BYTE) (msgFlag.area[i].radial >> 16);  
        gData[22+k] =  (BYTE) (msgFlag.area[i].radial >> 8);  
        gData[23+k] =  (BYTE) (msgFlag.area[i].radial >> 0); 

        for(j=0;j<6;j++)
        {
              gData[24+k+j] = msgFlag.area[i].startTime[j];           
              gData[30+k+j] = msgFlag.area[i].endTime[j];
            
        }
        gData[36+k] =  (BYTE) (msgFlag.area[i].speedTop >> 8);  
        gData[37+k] =  (BYTE) (msgFlag.area[i].speedTop >> 0); 
        
        gData[38+k] =  (BYTE) (msgFlag.area[i].speedTime >> 0);  

        k += 32;
    }
    
    len = k+6;
    
    return len;    
}

/**
  * @brief  行驶记录数据上传
  * @param  answerSerialID 应答流水号
  * @param  cmd 命令字
  * @param  data 数据块，数据块内容格式见GB/T 19056 中相关内容，包含GB/T 19056 要求的完整数据包，可为空。
  * @param  length 数据块长度
  * @retval length of data
  */
static int dev_run_record_ret(WORD answerSerialID,BYTE cmd,BYTE *data,WORD length)
{
    int len =-1;
    int i;
    
    WORD msgID = DEV_CMD_RUN_RECORD_RET;
	gData[0] = (BYTE) (msgID>>8);
    gData[1] = (BYTE) (msgID);  

    gData[2] = (BYTE) (answerSerialID>>8);
    gData[3] = (BYTE) (answerSerialID);  
    
	gData[4] = cmd; 
	for(i=0;i<length;i++)
        gData[5+i] = data[i];
    
    len = 5+length;
    
    return len;    
}


/**
  * @brief  电子运单上报
  * @param  waybill 电子运单内容
  * @param  length 电子运单长度
  * @retval length of data
  */
static int dev_waybill_ret(STRING waybill,DWORD length)
{
    int len =-1;
    int i;
    WORD msgID = DEV_CMD_WAYBILL_RET;
	gData[0] = (BYTE) (msgID>>8);
    gData[1] = (BYTE) (msgID);  

    for(i=0;i<length;i++)
    {
        gData[2+i] = waybill[i];
    }
    
    len = 2+length;
    
    return len;    
}


/**
  * @brief  驾驶员身份信息采集上报
  * @param  waybill 电子运单内容
  * @param  length 电子运单长度
  * @retval length of data
  */
static int dev_driver_ret(STR_DRIVER driver)
{
    int len =-1;
    int i,j;
    WORD msgID = DEV_CMD_DRIVER_RET;
	gData[0] = (BYTE) (msgID>>8);
    gData[1] = (BYTE) (msgID);  
    
    gData[2] = driver.status;
    
    for(i=0;i<6;i++)
        gData[3+i] = driver.time[i];
    
    gData[9] = driver.read_card_result;
    gData[10] = driver.name_len;
    
    for(i=0;i<driver.name_len;i++)
         gData[11+i] = driver.name[i];
    j = driver.name_len;
    
    for(i=0;i<20;i++)
        gData[11+j+i] = driver.certification_code[i];
    
    gData[31+j] = driver.certification_len;
    
    for(i=0;i<driver.certification_len;i++)
        gData[32+i+j] = driver.certification_name[i];
        
    j += driver.certification_len;
    
    for(i=0;i<4;i++)
         gData[32+i+j] = driver.deadline[i];
        
    len = j+32;
    
    return len;    
}



/**
  * @brief  定位数据批量上传
  * @param  dataUpload 定位数据
  * @retval length of data
  */
static int dev_data_upload(STR_DATA_UPLOAD dataUpload)
{
    int len =-1;
    int i,j,k;
    WORD msgID = DEV_CMD_DATA_UPLOAD;
	gData[0] = (BYTE) (msgID>>8);
    gData[1] = (BYTE) (msgID);  

    gData[2] = (BYTE)(dataUpload.dataCount>>8);
    gData[3] = (BYTE)(dataUpload.dataCount>>0);
    
    gData[4] = (BYTE)(dataUpload.dataType>>0); 
    
    k=0;
    for(i=0;i<dataUpload.dataCount;i++)
    {
            gData[5+k] = (BYTE)(dataUpload.data[i].dataLen>>8);
            gData[6+k] = (BYTE)(dataUpload.data[i].dataLen>>0); 
            for(j=0;j<dataUpload.data[i].dataLen;j++)
            {
                gData[7+k+j] = dataUpload.data[i].data[j];
            }
            k = 2+ dataUpload.data[i].dataLen;
    }
    
    len = 5+ k;
    return len;    
}


/**
  * @brief  CAN 总线数据上传
  * @param  dataUpload 定位数据
  * @retval length of data
  */
static int dev_can_data_upload(STR_DATA_CAN_UPLOAD canData)
{
    int len =-1;
    int i,j,k;
    WORD msgID = DEV_CMD_CAN_DATA_UPLOAD;
	gData[0] = (BYTE) (msgID>>8);
    gData[1] = (BYTE) (msgID);  

    gData[2] = (BYTE)(canData.dataCount>>8);
    gData[3] = (BYTE)(canData.dataCount>>0); 
    for(i=0;i<5;i++)
        gData[4+i] = canData.dataReceiveTime[i];
    k=0;
    for(i=0;i<canData.dataCount;i++)
    {
        for(j=0;j<4;j++)
            gData[9+i+k] = canData.can[i].can_id[j];
        for(j=0;j<8;j++)
            gData[13+i+k] = canData.can[i].can_id[j];
        k += 12;
    }
    
    len = 9+k;
    return len;    
}



/**
  * @brief  多媒体数据上传
  * @param  dataUpload 定位数据
  * @retval length of data
  */
static int dev_media_data_upload(STR_MEDIA media)
{
    int len =-1;
    int i,ret;
    WORD msgID = DEV_CMD_MEDIA_DATA_UPLOAD;
	gData[0] = (BYTE) (msgID>>8);
    gData[1] = (BYTE) (msgID);  

    gData[2] = (BYTE)(media.id>>24);
    gData[3] = (BYTE)(media.id>>16); 
    gData[4] = (BYTE)(media.id>>8);
    gData[5] = (BYTE)(media.id>>0);
    
    gData[6] = media.type;
    gData[7] = media.media_encode;    
    gData[8] = media.event_encode;
    gData[9] = media.channel_id;  
    for(i=0;i<28;i++)
        gData[10+i] = media.location_msg[i];
    ret = strlen(media.media_package);
    if(ret>=0)
    {
        for(i=0;i<ret;i++)
             gData[38+i] = media.media_package[i];
    }
    else
        return -1;
   
    len = ret+38;
    return len;    
}

/**
  * @brief  存储多媒体数据检索
  * @param  mediaCheck 多媒体数据
  * @retval length of data
  */
static int dev_media_check_upload(STR_MEDIA_CHECK mediaCheck)
{
    int len =-1;
    int i,j,k;
    WORD msgID = DEV_CMD_MEDIA_CHECK_UPLOAD;
	gData[0] = (BYTE) (msgID>>8);
    gData[1] = (BYTE) (msgID);  

    gData[2] = (BYTE)(mediaCheck.serialID>>24);
    gData[3] = (BYTE)(mediaCheck.serialID>>16); 
    gData[4] = (BYTE)(mediaCheck.serialID>>8);
    gData[5] = (BYTE)(mediaCheck.serialID>>0);

    gData[6] = (BYTE)(mediaCheck.dataCount>>24);
    gData[7] = (BYTE)(mediaCheck.dataCount>>16); 
    gData[8] = (BYTE)(mediaCheck.dataCount>>8);
    gData[9] = (BYTE)(mediaCheck.dataCount>>0);
    
    k=0;
    for(i=0;i<mediaCheck.dataCount;i++)
    {
        gData[10+k] = (BYTE)(mediaCheck.data[i].id>>24);
        gData[11+k] = (BYTE)(mediaCheck.data[i].id>>16);
        gData[12+k] = (BYTE)(mediaCheck.data[i].id>>8);
        gData[13+k] = (BYTE)(mediaCheck.data[i].id>>0);   

        gData[14+k] = mediaCheck.data[i].type;     
        gData[15+k] = mediaCheck.data[i].channel_id;
        gData[16+k] = mediaCheck.data[i].event_encode;        

        
        for(j=0;j<28;j++)
            gData[17+k+j] = mediaCheck.data[i].location_msg[j];
        k = k+7+28;
    }
    
    len = k+ 10;
    return len;    
}



/**
  * @brief  数据上行透传
  * @param  mediaCheck 多媒体数据
  * @retval length of data
  */
static int dev_trans_data(BYTE type, BYTE *data,WORD length)
{
    int len =-1;
    int i;
    
    WORD msgID = DEV_CMD_TRANS_DATA;
	gData[0] = (BYTE) (msgID>>8);
    gData[1] = (BYTE) (msgID); 
    gData[2] = type;
    for(i=0;i<length;i++)
        gData[3+i] = data[i];
    
    len = 3+length;
    
    return len;    
}    

/**
  * @brief  终端RSA 公钥
  * @param  rsaE 终端RSA 公钥{e,n}中的e
  * @param  rsaN RSA 公钥{e,n}中的n
  * @retval length of data
  */
static int dev_RSA(DWORD rsaE, BYTE *rsaN)
{
    int len =-1;
    int i;
    
    WORD msgID = DEV_CMD_RSA;
	gData[0] = (BYTE) (msgID>>8);
    gData[1] = (BYTE) (msgID); 
    
    gData[2] = (BYTE)(rsaE>>24);
    gData[3] = (BYTE)(rsaE>>16); 
    gData[4] = (BYTE)(rsaE>>8);
    gData[5] = (BYTE)(rsaE>>0);
    
    for(i=0;i<128;i++)
     gData[6+i] = rsaN[i];
    
    len = 6+128;
    
    return len;    
}

/**
  * @brief  数据压缩上报
  * @param  rarData 压缩消息体
  * @param  rarLen 压缩消息长度
  * @retval length of data
  */
static int dev_RAR(BYTE *rarData, DWORD rarLen)
{
    int len =-1;
    int i;
    
    WORD msgID = DEV_CMD_RAR;
	gData[0] = (BYTE) (msgID>>8);
    gData[1] = (BYTE) (msgID); 
    
    gData[2] = (BYTE)(rarLen>>24);
    gData[3] = (BYTE)(rarLen>>16); 
    gData[4] = (BYTE)(rarLen>>8);
    gData[5] = (BYTE)(rarLen>>0);    
    
    for(i=0;i<rarLen;i++)
        gData[6+i] = rarData[i];
   
    
    len = 6+rarLen;
    
    return len;    
}



/**
  * @brief  数据发送
  * @param  len 消息数据的长度
  * @retval 
    *@arg 0 成功
    *@arg <0 失败
  */
int dataSend(int len)
{
    int ret = -1;
    int i,j;
    
    
    BYTE data[DATA_LEN*2];
    BYTE check = gData[0];
    WORD dataLen;
    
    data[0] = MSG_FLAG;
    
    j=0;
    for(i=0;i<len;i++)
    {
        if(gData[j] == MSG_FLAG)
        {
            data[j] = 0x7d;
            data[j+1] = 0x02;
            j += 2;
        }
        else  if(gData[i] == (MSG_FLAG-1))
        {
            data[j] = 0x7d;
            data[j+1] = 0x01;
            j += 2;
        }
        else
        {
            data[j] =gData[i];
            j++;
        }
        if(j>=DATA_LEN*2)
            return -1;
    }
    /* calulate the check */
    for(i=2;i<j;i++)
    {
        check ^= data[i];
    }
    data[j] = check;
    data[j+1] = MSG_FLAG;
    
    dataLen = j+2;    
    
    /********************************这里添加具体的发送函数，加密算法在这里对数据进行加密********************************/
    
    ret =0;
    return ret;
}


/**
  * @brief  数据业务逻辑处理
  * @param  data 数据内容
  * @param  msgID 消息ID
  * @retval =0 成功  <0 失败
  */
int dataCmdProcess(BYTE *data ,WORD msgID)
{
    int ret = -1;
    
    switch(msgID)
    {
    case DEV_CMD_RESPONSE:
    {
        break;
    }
    case DEV_CMD_HEART_BEAT:
    {
        break;
    }
    case DEV_CMD_UNREGISTER:
    {
        break;
    }
    case DEV_CMD_AUTHENTICATION:
    {
        break;
    }
    case DEV_CMD_PARAMETER:
    {
        break;
    }
    case DEV_CMD_ATTRIBUTE:
    {
        break;
    }
    case DEV_CMD_UPLOAD_RESULT:
    {
        break;
    }
    case DEV_CMD_LOCATION:
    {
        break;
    }
    case DEV_CMD_EVENT_REPORT:
    {
        break;
    }
    case DEV_CMD_ASK_ANSWER:
    {
        break;
    }

    case DEV_CMD_MSG_SET:
    {
        break;
    }
    case DEV_CMD_CONTROL_CAR_RET:
    {
        break;
    }
    case DEV_CMD_RUN_RECORD_RET:
    {
        break;
    }
    case DEV_CMD_WAYBILL_RET:
    {
        break;
    }
    case DEV_CMD_DRIVER_RET:
    {
        break;
    }    
    case DEV_CMD_DATA_UPLOAD:
    {
        break;
    }
    case DEV_CMD_CAN_DATA_UPLOAD:
    {
        break;
    }
    case DEV_CMD_MEDIA_DATA_UPLOAD:
    {
        break;
    }
    case DEV_CMD_MEDIA_CHECK_UPLOAD:
    {
        break;
    }
    case DEV_CMD_TRANS_DATA:
    {
        break;
    }     
    case DEV_CMD_RSA:
    {
        break;
    }
    case DEV_CMD_RAR:
    {
        break;
    } 
    default:
    {
        
    }
    
    }// end of switch
    
    return ret;
}

/**
  * @brief  接收数据处理
  * @param  data 接收内容
  * @param  len 接收内容长度
  * @retval =0 成功  <0 失败
  * @attention 接收收据不包括头尾的标志位
  */
int dataReceiveProcess(BYTE *data,WORD len)
{
    int ret = -1;
    int i;
    BYTE check = data[0];
    WORD cmd;
    
    /* calulate the check */
    for(i=1;i<len;i++)
    {
        check ^= data[i];
    }    
    if(check != data[len-1])
        return -1;
    
    cmd = (data[0]<<8) + data[1];
    
    ret = dataCmdProcess(&data[2],cmd);
   
    return ret;
}

/**
  * @brief  是否需要转换数据
  * @param  data 要判断的数据
  * @retval ERROR_CODE
  */
ERROR_CODE isNeedToConvert(BYTE data)
{
	if((data == 0X7E) || (data == (0X7E-1) ))	
		return RET_OK;
	return RET_FAILED;
}

/**
  * @brief  重传时间计算
  * @param  timeLast  上一次重传时间
  * @param  timeCurrent 当前重传时间
  * @param  retryTimes 重传次数
  * @retval ERROR_CODE
  */
ERROR_CODE msgGetRetryTime(WORD *timeLast,WORD *timeCurrent,WORD *retryTimes)
{
	ERROR_CODE ret = RET_FAILED;
	*timeCurrent = *timeLast * (*retryTimes+1);
	*retryTimes = *retryTimes+1;
	
	ret = RET_OK;
	
	return ret;
}

#endif //end of JTT808_MODULE




